package cn.wizzer.app.task.commons.ext.quartz.job;

import cn.wizzer.app.sys.modules.services.SysTaskService;
import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.services.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Times;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import java.util.List;


/**
 * 删除游客
 *
 * @author .
 */
@IocBean
public class DelVisitorJob implements Job {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    protected SysTaskService sysTaskService;
    @Inject
    @Reference(retries = 3, timeout = 500000)
    private UserService userService;

    @Override
    public void execute(JobExecutionContext context) {
        log.info("删除游客账号开始");
        // 游客账号数据保存5天
        long time = Times.getTS() - 5 * 24 * 60 * 60;
        Cnd cnd = Cnd.where("visitor", "=", 1).and("ctAt", "<=" ,time);
        List<User> visitors = userService.query(cnd);
        log.info("删除游客账号id集合：" + visitors);
        for (User user : visitors) {
            userService.deleteVisitor(user.getId());
        }
        String taskId = context.getJobDetail().getKey().getName();
        log.info("删除游客账号结束");
        sysTaskService.update(Chain.make("exeAt", (int) (System.currentTimeMillis() / 1000))
                .add("exeResult", "执行成功"), Cnd.where("id", "=", taskId));
    }
}
